<template>
  <van-form @submit="onSubmit">
    <van-field
      v-model="username"
      name="username"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      type="password"
      name="password"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
    <div style="margin: 16px">
      <van-button round block type="info" native-type="submit">提交</van-button>
    </div>
  </van-form>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    ...mapMutations(['SET_LOGIN_INFO']),
    async onSubmit(values) {
      const res = await this.$http.post('/api/login', values)
      const data = res.data.data
      this.SET_LOGIN_INFO({ token: data.token, userInfo: data.userInfo })
      // 基于携带的这个导航守卫跳转过来的参数，进行跳转
      let fullpath = this.$route.query.fullPath || '/layout/readHome'
      this.$router.push(fullpath)
    },
  },
}
</script>

<style></style>
